<?php
class Broadcast_model extends CI_Model {

	/**
	 * @author Pwint Phyu Shwe
	 * Address Model for property_address
	 * Saving property address for advertisement
	 * Refer detail in each function's comment section below.
	 */
	// table name
	private $tbl_broadcast = 'broadcast';
	private $tbl_user = 'masteruser';

	function __construct()
	{
		// Call the Model constructor
		parent::__construct();
	}
	// get number of broadcast messages in database
	function count_all(){
		return $this->db->count_all($this->tbl_broadcast);
	}	
	// get address by userid
	function get_by_userid($userID){
		$this->db->where('userid', $userID);
		$this->db->order_by("posted", "desc");
		$query =  $this->db->get($this->tbl_broadcast);
		
		return $query->result();
	}
	// get address by id
	function get_data($cond = NULL){
		//filter
		if ($cond != NULL){
			foreach ($cond as  $param => $item)
				$this->db->where($param , $item);
		}
		$this->db->select('*', 'masteruser.firstName');
		$this->db->from($this->tbl_broadcast);
		$this->db->join($this->tbl_user, 'masteruser.userID = broadcast.senderID', 'left');
		$this->db->order_by("posted", "desc");
		$query =  $this->db->get();
		
		return $query->result();
	}

	//this function will dipslay in Message Inbox
	function get_other_msg($userid){
		
		$this->db->select('*', 'masteruser.firstName');
		$this->db->from($this->tbl_broadcast);
		$this->db->join($this->tbl_user, 'masteruser.userID = broadcast.senderID', 'left');

		$this->db->where('senderID !=', $userid);
		$this->db->order_by("posted", "desc");
		$query =  $this->db->get();
		return $query->result();
	}
	// add new broadcast
	function save($broadcast){
		$this->db->insert($this->tbl_broadcast, $broadcast);
		//return $this->get_by_userid($this->db->insert_id());
		$cond['senderID'] = $broadcast['senderID'];
		return $this->get_data($cond);
	}
	// delete broadcast by id
	function delete($id){
		$this->db->where('id', $id);
		$this->db->delete($this->$tbl_broadcast);
	}
}
?>